package etnorservice.etnor_service.controller;

import etnorservice.etnor_service.domain.MenuItemDomain;
import etnorservice.etnor_service.result.Result;
import etnorservice.etnor_service.service.MenuItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2025/03/24/14:05
 **/
@RestController
public class PermissionController {

    @Autowired
    private MenuItemService menuItemService;

    @GetMapping("/system/permission/tree")
    public Result getPermissionTree() {
        List<MenuItemDomain> tree = menuItemService.getAllPermissions();
        return Result.success(tree);
    }

    @GetMapping("/system/permission/role/{roleId}")
    public Result getRolePermissions(@PathVariable Integer roleId) {
        List<MenuItemDomain> permissions = menuItemService.getRolePermissions(roleId);
        return Result.success(permissions);
    }

    @PostMapping("/system/permission/role/{roleId}")
    public Result assignPermissions(@PathVariable Integer roleId,
                                    @RequestBody List<Integer> permissionIds) {
        menuItemService.assignPermissions(roleId, permissionIds);
        return Result.success(null);
    }

    @PostMapping("/system/permission")
    public Result addPermission(@RequestBody MenuItemDomain permission) {
        MenuItemDomain added = menuItemService.addPermission(permission);
        return Result.success(added);
    }

    @PutMapping("/system/permission/{id}")
    public Result updatePermission(@PathVariable Integer id,
                                   @RequestBody MenuItemDomain permission) {
        permission.setId(id);
        menuItemService.updatePermission(permission);
        return Result.success(null);
    }

    @DeleteMapping("/{id}")
    public Result deletePermission(@PathVariable Integer id) {
        menuItemService.deletePermission(id);
        return Result.success(null);
    }
}